<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
    <link rel="stylesheet" href="/element-ui/index.css">
    <link rel="stylesheet" href="/element-ui/fonts/element-icons.css">
    <script src="/element-ui/vue.js"></script>
    <script src="/element-ui/index.js"></script>
    <script src="/element-ui/axios.min.js"></script>
    <style>
        body {
            font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', '\\5FAE\\8F6F\\96C5\\9ED1', Arial, sans-serif;
            background-color: #f5f6fa;
        }
        #app {
            padding: 20px;
            background-color: #f5f6fa;
        }
        .page-title {
            font-size: 26px;
            font-weight: bold;
            color: #333;
            background: #fff;
            padding: 24px 20px 12px 20px;
            border-bottom: 1px solid #eee;
            margin-bottom: 20px;
            letter-spacing: 2px;
            text-align: center;
            animation: fadeInDown 0.8s ease-out;
            box-shadow: 0 2px 8px rgba(0,0,0,0.05);
            border-radius: 8px;
        }
        @keyframes fadeInDown {
            from { opacity: 0; transform: translateY(-20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        .el-main {
            padding: 20px;
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.05);
        }
        .el-form-item {
            margin-bottom: 20px;
        }
        .el-input__inner,
        .el-select .el-input__inner {
            transition: all 0.3s ease;
        }
        .el-input__inner:focus,
        .el-input__inner:hover,
        .el-select .el-input__inner:focus,
        .el-select .el-input__inner:hover {
            border-color: #409EFF;
            box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
        }
        .el-button {
            transition: all 0.3s ease;
        }
        .el-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 10px rgba(64, 158, 255, 0.2);
        }
        .el-button--primary:hover {
            box-shadow: 0 4px 10px rgba(64, 158, 255, 0.2);
        }
        .el-button--success:hover {
            box-shadow: 0 4px 10px rgba(103, 194, 58, 0.2);
        }

        .el-table {
            animation: fadeIn 1s ease-out;
        }
        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }
        .el-table__row {
            transition: all 0.3s ease;
        }
        .el-table__row:hover {
            background-color: #f5f7fa !important;
            transform: scale(1.005);
            box-shadow: 0 2px 8px rgba(0,0,0,0.05);
        }
        .pagination-container {
            margin-top: 20px;
            text-align: right;
            padding: 10px 0;
            background: #fff;
            border-top: 1px solid #eee;
            border-radius: 0 0 8px 8px;
        }
        .el-pagination button,
        .el-pagination .btn-prev,
        .el-pagination .btn-next,
        .el-pagination .el-pager li {
            transition: all 0.3s ease;
        }
        .el-pagination button:hover,
        .el-pagination .btn-prev:hover,
        .el-pagination .btn-next:hover,
        .el-pagination .el-pager li:hover {
            color: #409EFF;
            transform: translateY(-1px);
        }
    </style>
</head>
<body>
<div id="app">
    <div class="page-title">用户管理</div>
    <el-container>
        <el-main>
            <!-- 搜索表单 -->
            <el-form :inline="true" :model="searchForm" class="demo-form-inline">
                <el-form-item label="姓名">
                    <el-input v-model="searchForm.realName" placeholder="请输入姓名"></el-input>
                </el-form-item>
                <el-form-item label="院系">
                    <el-select v-model="searchForm.department" placeholder="请选择院系">
                        <el-option label="全部" value=""></el-option>
                        <el-option
                            v-for="item in departments"
                            :key="item"
                            :label="item"
                            :value="item">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="search">查询</el-button>
                    <el-button @click="resetForm">重置</el-button>
                    <el-button type="success" @click="add">新增</el-button>
                </el-form-item>
            </el-form>

            <!-- 用户表格 -->
            <el-table :data="tableData" border style="width: 100%; margin-top: 20px;" v-loading="loading">
                <el-table-column prop="username" label="用户名"></el-table-column>
                <el-table-column prop="password" label="密码"></el-table-column>
                <el-table-column prop="realName" label="姓名"></el-table-column>
                <el-table-column prop="department" label="院系"></el-table-column>
                <el-table-column prop="phone" label="电话"></el-table-column>
                <el-table-column label="用户状态" width="100">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.status === 1" type="success">管理员</el-tag>
                        <el-tag v-else type="info">普通用户</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180">
                    <template slot-scope="scope">
                        <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 分页 -->
            <div class="pagination-container">
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[10, 20, 30, 50]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </div>
        </el-main>
    </el-container>
</div>
<script>
    new Vue({
        el: '#app',
        data() {
            return {
                searchForm: {
                    realName: '',
                    department: ''
                },
                tableData: [],
                currentPage: 1,
                pageSize: 10,
                total: 0,
                loading: false,
                departments: [
                    '计算机系', '金融系', '电子商务系', '大数据系', '人工智能系',
                        '艺术设计系', '医学系', '环境设计系', '音乐系', '老年管理系'
                ]
            }
        },
        created() {
            this.fetchData();
        },
        methods: {
            fetchData() {
                this.loading = true;
                const params = {
                    pageNum: this.currentPage,
                    pageSize: this.pageSize,
                    ...this.searchForm
                };
                axios.get('/user/api/list', { params })
                    .then(response => {
                        this.tableData = response.data.list;
                        this.total = response.data.total;
                    })
                    .catch(error => {
                        console.error('Error fetching data:', error);
                        this.$message.error('获取数据失败');
                    })
                    .finally(() => {
                        this.loading = false;
                    });
            },
            search() {
                this.currentPage = 1;
                this.fetchData();
            },
            resetForm() {
                this.searchForm = {
                    realName: '',
                    department: ''
                };
                this.search();
            },
            handleSizeChange(val) {
                this.pageSize = val;
                this.fetchData();
            },
            handleCurrentChange(val) {
                this.currentPage = val;
                this.fetchData();
            },
            handleDelete(row) {
                this.$confirm('确认删除该用户?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.post(`/user/delete/${row.id}`)
                        .then(response => {
                            if (response.data === 'success') {
                                this.$message.success('删除成功');
                                this.fetchData();
                            } else {
                                this.$message.error('删除失败');
                            }
                        })
                        .catch(error => {
                            console.error('Error deleting user:', error);
                            this.$message.error('删除失败');
                        });
                }).catch(() => {
                    this.$message.info('已取消删除');
                });
            },
            add() {
                window.location.href = '/user/add';
            },
            handleEdit(row) {
                window.location.href = `/user/edit/${row.id}`;
            }
        }
    });
</script>
</body>
</html> 